package com.ruoyi.system.services;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.config.GeoConfig;
import com.ruoyi.common.enums.YnStatusEnum;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.KeyPersonnelAdmin;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.system.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 菜单 业务层处理
 * 
 * @author ruoyi
 */
@Service
public class PersonPositionServices
{
    @Autowired
    private SysUserMapper userMapper;
    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private KeyPersonnelAdminService keyPersonnelAdminService;

    /**
     * 查询人员定位系统所有用户
     *
     * @param type  员工类型:1.内部 2.访客 3.承包商
     * @param pageSize
     * @return
     */
    public String getStaffUserList(Integer type, Integer pageSize) {
        Map<String, Object> map = new HashMap<>(10);
        map.put("type", type);
        int size = pageSize==null ? 10000 : pageSize;
        map.put("orgId", 100);
        map.put("pageNum", 1);
        map.put("pageSize", size);  // 超时时间（秒）
        return GeoConfig.sendPost(GeoConfig.allStaffUrl, map);
    }


    public List<KeyPersonnelAdmin> synPersonnel() {
        // 查询内部所有人
        KeyPersonnelAdmin search = new KeyPersonnelAdmin();
        search.setYn(YnStatusEnum.Y.getCode());
        search.setIsUpload(0);
        List<KeyPersonnelAdmin> personList1 = keyPersonnelAdminService.selectList(search);
        if (StringUtils.isEmpty(personList1)) {
            return null;
        }

        // 查询定位系统所有人
        List<KeyPersonnelAdmin> personList2 = getPersons();

        return handlePersonInfo(personList1, personList2);
    }

    /**
     * 查询定位系统的所有用户
     *
     * @return
     */
    public List<KeyPersonnelAdmin> getPersons() {
        String synPersons = getStaffUserList(1, 1000);
        if (StringUtils.isEmpty(synPersons)) {
            return null;
        }

        JSONObject jsonObject = JSONObject.parseObject(synPersons);
        JSONArray persons = jsonObject.getJSONObject("data")
                .getJSONArray("records");
        if (persons==null || persons.isEmpty()) {
            return null;
        }

        return persons.stream().map(person -> {
            JSONObject jsonObject1 = (JSONObject) person;
            int uid = jsonObject1.getIntValue("id");
            String name = jsonObject1.getString("name");
            String phone = jsonObject1.getString("phone");
            String tagId = jsonObject1.getString("tagId");
            String jobNumber = jsonObject1.getString("jobNumber");

            KeyPersonnelAdmin entity2 = new KeyPersonnelAdmin();
            entity2.setName(name);
            entity2.setPositionUserId(uid);
            entity2.setPositionPhone(phone);
            entity2.setPositionTagId(tagId);
            entity2.setPositionJobNumber(jobNumber);
            return entity2;
        }).collect(Collectors.toList());
    }

    public List<KeyPersonnelAdmin> handlePersonInfo(List<KeyPersonnelAdmin> personList1, List<KeyPersonnelAdmin> personList2) {
        List<KeyPersonnelAdmin> newPersonList = new ArrayList<KeyPersonnelAdmin>();

        for (KeyPersonnelAdmin person2 : personList2) {
            KeyPersonnelAdmin newPerson = null;
            int i = 0;

            for (KeyPersonnelAdmin person1 : personList1) {
                if (person1.getName().equals(person2.getName())) {
                    person1.setPositionPhone(person2.getPositionPhone());
                    person1.setPositionTagId(person2.getPositionTagId());
                    person1.setPositionJobNumber(person2.getPositionJobNumber());
                    person1.setPositionUserId(person2.getPositionUserId());
                    newPerson = person1;
                    i++;
                }
            }
            // i>1，重复数据不处理
            if (i==1) {
                newPersonList.add(newPerson);
            }
        }

        return newPersonList;
    }

    public String getPersonPosition(String tagId, Long endTime) {
        Map<String, Object> map = new HashMap<>(10);
        map.put("endTime", endTime);
        map.put("tagId", tagId);
        return GeoConfig.sendPost(GeoConfig.positionUrl, map);
    }
}
